package com.reptile.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import redis.clients.jedis.JedisCluster;

import com.reptile.domain.Taobao;
import com.reptile.mapper.TaobaoMapper;
import com.reptile.service.TaobaoService;
import com.reptile.tool.MD5Tool;

@Service
public class TaobaoServiceImpl implements TaobaoService
{
	@Autowired
	private TaobaoMapper taobaomapper;
	
	@Autowired
	private JedisCluster jedisCluster;
	
	
	//final static String repeatKey="RepeatKey";
	
	//final static String waitKey="WaitKey";
	
	public void TaobaoSave(Taobao taobao)
	{
		taobaomapper.inster(taobao);
	}
	
	@Cacheable(value="getAll")
	public List<Taobao> TaobaoFind()
	{
		System.out.println("开始redis缓存了");
		return taobaomapper.getAll();
	}
	

	//如果没有爬过此url，就加入待爬集合中
	public void RedisSaveSet(String url)
	{
		//MD5压缩url
		String md5url=MD5Tool.md5(url);
		if(!jedisCluster.sismember(md5url, "0"))
		{
				jedisCluster.sadd(md5url, "1");
		}

	}
	
	//url爬取完了，丢到已爬取集合
	public void RepeatUrl(String url)
	{
		String md5url=MD5Tool.md5(url);
		jedisCluster.sadd(md5url, "0");
	}
}
